package com.saturn.action.sldb.discussion;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.saturn.app.utils.DateUtils;
import com.saturn.app.web.IAction;
import com.saturn.app.web.IView;
import com.saturn.app.web.view.JspView;
import com.saturn.sldb.Discussion;

public class QueryTemp2Action implements IAction{
	@Override
	public IView execute(HttpServletRequest request,
			HttpServletResponse response) {
		
		String view = request.getParameter("view");
		String id = request.getParameter("id");
		Discussion vo = Discussion.get(id);
		
		String inputType = vo.getInputType();
		if("自动".equals(inputType)){
			request.setAttribute("discussion", vo);
			if("ok".equals(view)){
				return new JspView("/app/sldb/discussion/approval/view.jsp");
			}else if("files1".equals(view)){
				request.setAttribute("discussion", vo);
				String type = "1"; //如何type为1代表申报的时候上传附件。
				return new JspView("/app/sldb/discussion/apply/upfileByAuto.jsp?type="+type);
			}else if("files3".equals(view)){
				request.setAttribute("discussion", vo);
				String type = "0"; //如何type为0代表申报的时候上传附件。
				return new JspView("/app/sldb/discussion/apply/upfileByAuto.jsp?type="+type);
			}else if("files".equals(view)){
				request.setAttribute("discussion", vo);
				String type = "2";//如何type为2代表审核的时候上传附件。
				return new JspView("/app/sldb/discussion/apply/upfileByAuto.jsp?type="+type);
			}else if("files2".equals(view)){
				SimpleDateFormat form=new SimpleDateFormat("yyyyMMdd");
				String createTimeFormat="";
				try {
					createTimeFormat = form.format(DateFormat.getDateInstance().parse(vo.getCreateTime()));
				} catch (ParseException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				String nowTimeStr = DateUtils.getSystemDate();
				String nowTimeFormat="";
				try {
					nowTimeFormat = form.format(DateFormat.getDateInstance().parse(nowTimeStr));
				} catch (ParseException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				int createTime = Integer.parseInt(createTimeFormat);
				int nowTime = Integer.parseInt(nowTimeFormat);
				if(nowTime-createTime>=200){
					return new JspView("/app/sldb/discussion/apply/timeOut.jsp");
				}else{
					request.setAttribute("discussion", vo);
					String type = "3";//如何type为2代表审核的时候上传附件。
					return new JspView("/app/sldb/discussion/apply/upfileByAuto.jsp?type="+type);
				}
			}else if("files4".equals(view)){
				SimpleDateFormat form=new SimpleDateFormat("yyyyMMdd");
				String createTimeFormat="";
				try {
					createTimeFormat = form.format(DateFormat.getDateInstance().parse(vo.getCreateTime()));
				} catch (ParseException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				String nowTimeStr = DateUtils.getSystemDate();
				String nowTimeFormat="";
				try {
					nowTimeFormat = form.format(DateFormat.getDateInstance().parse(nowTimeStr));
				} catch (ParseException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				int createTime = Integer.parseInt(createTimeFormat);
				int nowTime = Integer.parseInt(nowTimeFormat);
				if(nowTime-createTime>=200){
					return new JspView("/app/sldb/discussion/apply/timeOut.jsp");
				}else{
					request.setAttribute("discussion", vo);
					String type = "4";//如何type为4代表审批时历史记录上传附件。
					return new JspView("/app/sldb/discussion/apply/upfileByAuto.jsp?type="+type);
				}
				
			}else{
				return new JspView("/app/sldb/temp/upfile.jsp");
			}
		}else{
			request.setAttribute("discussion", vo);
			if("ok".equals(view)){
				return new JspView("/app/sldb/discussion/approval/view.jsp");
			}if("files".equals(view)){
				request.setAttribute("discussion", vo);
				return new JspView("/app/sldb/discussion/apply/upfile.jsp");
			}else if("files1".equals(view)){
				request.setAttribute("discussion", vo);
				String type = "1"; //如何type为1代表申报的时候上传附件。
				return new JspView("/app/sldb/discussion/apply/upfile.jsp?type="+type);
			}else if("files2".equals(view)){
				SimpleDateFormat form=new SimpleDateFormat("yyyyMMdd");
				String createTimeFormat="";
				try {
					createTimeFormat = form.format(DateFormat.getDateInstance().parse(vo.getCreateTime()));
				} catch (ParseException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				String nowTimeStr = DateUtils.getSystemDate();
				String nowTimeFormat="";
				try {
					nowTimeFormat = form.format(DateFormat.getDateInstance().parse(nowTimeStr));
				} catch (ParseException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				int createTime = Integer.parseInt(createTimeFormat);
				int nowTime = Integer.parseInt(nowTimeFormat);
				if(nowTime-createTime>=200){
					return new JspView("/app/sldb/discussion/apply/timeOut.jsp");
				}else{
					request.setAttribute("discussion", vo);
					String type = "3";//如何type为3代表历史记录的时候上传附件。
					return new JspView("/app/sldb/discussion/apply/upfileByAuto.jsp?type="+type);
				}
				
			}else if("files4".equals(view)){
				SimpleDateFormat form=new SimpleDateFormat("yyyyMMdd");
				String createTimeFormat="";
				try {
					createTimeFormat = form.format(DateFormat.getDateInstance().parse(vo.getCreateTime()));
				} catch (ParseException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				String nowTimeStr = DateUtils.getSystemDate();
				String nowTimeFormat="";
				try {
					nowTimeFormat = form.format(DateFormat.getDateInstance().parse(nowTimeStr));
				} catch (ParseException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				int createTime = Integer.parseInt(createTimeFormat);
				int nowTime = Integer.parseInt(nowTimeFormat);
				if(nowTime-createTime>=200){
					return new JspView("/app/sldb/discussion/apply/timeOut.jsp");
				}else{
					request.setAttribute("discussion", vo);
					String type = "4";//如何type为4代表审批时历史记录上传附件。
					return new JspView("/app/sldb/discussion/apply/upfileByAuto.jsp?type="+type);
				}
				
			}else{
				return new JspView("/app/sldb/temp/upfile.jsp");
			}
		}
	}
	
	@Override
	public String requestMapping() {
		return "/app/sldb/discussion/queryTemp2.action";
	}
}
